package com.boilerCloud.controller.sys;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import com.boilerCloud.dataSource.LogAnnotation;
import com.boilerCloud.dto.HzlqswPage;
import com.boilerCloud.dto.sys.ReqResult;
import com.boilerCloud.dto.sys.Role;
import com.boilerCloud.dto.sys.User;
import com.boilerCloud.service.sys.IHzlqswRoleService;
import com.boilerCloud.util.StringUtil;


@RestController
public class HzlqswRoleControler {
	@Resource
	private IHzlqswRoleService roleService;
	
	private static final Logger log = LoggerFactory.getLogger(HzlqswRoleControler.class);

	// 进入角色列表页面
	@RequestMapping(value = "/service/roleList")
	public ModelAndView menuList() {
		return new ModelAndView("/views/sys/roleList.html");
	}

	// 获取用户角色列表
	@RequestMapping(value = "/service/getRoleList", method = RequestMethod.POST, produces = "text/plain;charset=UTF-8")
	public ReqResult getRoleList(@RequestParam(required = true) String roleName, HttpServletRequest request) {
		ReqResult rst = new ReqResult();
		try {
			Role role = new Role();
			role.setROLENAME(roleName);
			User operator = (User) request.getSession().getAttribute("usersession");
			
			String yema = request.getParameter("yema");
			String pagelen = request.getParameter("pagelen");
			HzlqswPage<Role> page = new HzlqswPage<Role>();
			page.setCurPage(Integer.parseInt(yema));
			page.setPageSize(Integer.parseInt(pagelen));
		 
			if (StringUtil.isNum(yema) && StringUtil.isNum(pagelen)) {
				roleService.qryRoleList(role,page);
				rst.setReturnObject(page);
				rst.setResultCode(ReqResult.resultCode_Success);
				return rst;
			} else {
				rst.setResultCode(ReqResult.resultCode_param_error);
				rst.setReturnObject("参数格式错误");
				return rst;
			}

		} catch (Exception e) {
			log.error("获取角色列表",e);
			rst.setResultCode(ReqResult.resultCode_error);
			rst.setReturnObject("系统异常");
			return rst;
		}
	}

	// 添加菜单
	@LogAnnotation("新增角色")
	@RequestMapping(value = "/service/doAddRole", method = RequestMethod.POST, produces = "text/plain;charset=UTF-8")
	public ReqResult doAddRole(@RequestParam(required = true) String roleName,@RequestParam(required = true) String remarks,
			@RequestParam(required = true) String menus, HttpServletRequest request) {
		ReqResult rst = new ReqResult();
		try {
			if (roleName.isEmpty() || menus.isEmpty()) {
				rst.setResultCode(ReqResult.resultCode_param_lost);
				rst.setReturnObject("参数缺失");
				return rst;
			} else {
				Role role = new Role();
				role.setROLENAME(roleName);
				role.setREMARKS(remarks);
				User operator = (User) request.getSession().getAttribute("usersession");
			
				role.setMenuIDs(menus.split(","));
				roleService.addRole(role);
				rst.setResultCode(ReqResult.resultCode_Success);
				return rst;
			}
		} catch (Exception e) {
			log.error("添加角色列表",e);
			rst.setResultCode(ReqResult.resultCode_error);
			rst.setReturnObject("系统异常");
			return rst;
		}
	}

	// 删除角色
	@LogAnnotation("删除角色")
	@RequestMapping(value = "/service/delRole", method = RequestMethod.POST, produces = "text/plain;charset=UTF-8")
	public ReqResult delRole(@RequestParam(required = true) String roleID, HttpServletRequest request) {
		ReqResult rst = new ReqResult();
		try {
			if (roleID.isEmpty()) {
				rst.setResultCode(ReqResult.resultCode_param_lost);
				rst.setReturnObject("参数缺失");
				return rst;
			} else if (!StringUtil.isNum(roleID)) {
				rst.setResultCode(ReqResult.resultCode_param_error);
				rst.setReturnObject("参数格式错误");
				return rst;
			} else if (((User) request.getSession().getAttribute("usersession")).getRoleId() == Integer
					.parseInt(roleID)) {
				rst.setResultCode(ReqResult.resultCode_role_forbid);
				rst.setReturnObject("不能删除自己所属角色");
				return rst;
			} else {
				roleService.delRole(Integer.parseInt(roleID));
				rst.setResultCode(ReqResult.resultCode_Success);
				return rst;
			}
		} catch (Exception e) {
			log.error("删除角色列表",e);
			rst.setResultCode(ReqResult.resultCode_error);
			rst.setReturnObject("系统异常");
			return rst;
		}
	}

	// 预添加角色
	@RequestMapping(value = "/service/addRole", produces = "text/plain;charset=UTF-8")
	public ReqResult addRole() {
		ReqResult rst = new ReqResult();
		rst.setResultCode(ReqResult.resultCode_Success);
		return rst;
	}

	// 预修改角色
	@RequestMapping(value = "/service/editRole", produces = "text/plain;charset=UTF-8")
	public ReqResult editRole() {
		ReqResult rst = new ReqResult();
		rst.setResultCode(ReqResult.resultCode_Success);
		return rst;
	}

	// 修改角色
	@LogAnnotation("修改角色")
	@RequestMapping(value = "/service/doEditRole", method = RequestMethod.POST, produces = "text/plain;charset=UTF-8")
	public ReqResult doEditRole(@RequestParam(required = true) String viewmenuid,
			@RequestParam(required = true) String roleID, @RequestParam(required = true) String roleName,@RequestParam(required = true) String  remarks,
			@RequestParam(required = true) String menus, HttpServletRequest request) {
		ReqResult rst = new ReqResult();
		try {
			if (viewmenuid.isEmpty() || roleID.isEmpty() || roleName.isEmpty() || menus.isEmpty()) {
				rst.setResultCode(ReqResult.resultCode_param_lost);
				rst.setReturnObject("参数缺失");
				return rst;
			} else if (!StringUtil.isNum(roleID)) {
				rst.setResultCode(ReqResult.resultCode_param_error);
				rst.setReturnObject("参数格式错误");
				return rst;
			} else {
				Role role = new Role();
				role.setID(Integer.parseInt(roleID));
				role.setREMARKS(remarks);
				role.setROLENAME(roleName);
				role.setMenuIDs(menus.split(","));
				roleService.updateRole(role, viewmenuid);
				rst.setResultCode(ReqResult.resultCode_Success);
				return rst;
			}
		} catch (Exception e) {
			log.error("修改角色列表",e);
			rst.setResultCode(ReqResult.resultCode_error);
			rst.setReturnObject("系统异常");
			return rst;
		}
	}

	// 获取角色列表
	@RequestMapping(value = "/getRoles", method = RequestMethod.POST, produces = "text/plain;charset=UTF-8")
	public ReqResult getRoles(HttpServletRequest request) {
		ReqResult rst = new ReqResult();
		try {
			User operator = (User) request.getSession().getAttribute("usersession");
			//role.setOrganizationId(operator.getOrganizationId());
			rst.setReturnObject(roleService.qryRoles(0));
			rst.setResultCode(ReqResult.resultCode_Success);
			return rst;
		} catch (Exception e) {
			e.printStackTrace();
			rst.setResultCode(ReqResult.resultCode_error);
			rst.setReturnObject("系统异常");
			return rst;
		}
	}
}
